<?php
namespace V1;
class PushMessage extends \ResourceClass
{
    public $isActive = true;
    public $remark = '消息推送';

    public function post (array $req, array $post, &$result) {
      if (!isset($req['uid'])) {
        return 'require user id ';
      }
      if (!isset($req['userType'])) {
        return 'require user type';
      }
      if (!isset($req['k']) || !isset($req['t'])) {
        return 'data error';
      }
      if (!isset($post['payload'])) {
        return 'require payload';
      }
      $data = base64_decode($post['payload']);
      @parse_str($data,$data);
      if(!isset($data['title'])){
        return 'require message title';
      }
      if(!isset($data['content'])){
        return 'require message content';
      }
      if(!isset($data['type'])){
        return 'require message type';
      }
      if(!isset($data['receiver'])){
        return 'require message receiver';
      }
      $user_cls = \Biz\UserHandler::getById($req['userType'],$req['uid']);
      $enterprise_id = $user_cls->getEnterpriseId();
      /* 授权验证*/
      if (\Biz\UserHandler::checkAuth($req['userType'], $req['uid'],$enterprise_id, $req['id'].$req['t'], $req['k']) == false) {
          return 'data illegal';
      }
      $push_message_handler = new \Biz\PushMessageHandler();
      $type = (int) $data['type'];
      switch ($type) {
        case \Cls\PushMessage\PushMessage::MESSAGE_TYPE_SMS :
          if (empty($req['uid']) || empty($data['receiver']) || empty($data['content'])) {
            return false;
          }
          $push_message_cls = new \Cls\PushMessage\PushMessage();
          $push_message_cls->create($req['uid'],$data['receiver'],$data['content']);
          $push_message_cls->setTitle($data['title']);
          $push_message_cls->setType(\Cls\PushMessage\PushMessage::MESSAGE_TYPE_SMS);
          $push_message_handler->insert($push_message_cls);
          break;
        case \Cls\PushMessage\PushMessage::MESSAGE_TYPE_JPUSH :
          if (empty($req['uid']) || empty($data['receiver']) || empty($data['content'])) {
            return false;
          }
          $log_handler = new \Biz\LogHandler();
          $tabulation = new \Cls\Tabulation();
          $tabulation->setWhere('user_id',$data['receiver'])
            ->setLimit(1000000);
          $log_handler->getAppMessageListByUserId($data['receiver'],$tabulation);
          $result = $tabulation->getData();
          if (!empty($result)) {
            foreach ($result as $jpushId) {
              $push_message_cls = new \Cls\PushMessage\PushMessage();
              $push_message_cls->create($req['uid'],$jpushId,$data['content']);
              $push_message_cls->setTitle($data['title']);
              $push_message_cls->setType(\Cls\PushMessage\PushMessage::MESSAGE_TYPE_JPUSH);
              $push_message_handler->insert($push_message_cls);
            }
          }
          break;
        default :
          return 'unsupported message type';
      }
      return '';
    }
}
?>